package edu.neu.swc.cloud.controller;

import com.alibaba.fastjson.JSON;
import edu.neu.swc.cloud.domain.EquipCata;
import edu.neu.swc.cloud.domain.ProductCata;
import edu.neu.swc.cloud.domain.QueryInfo;
import edu.neu.swc.cloud.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/equipType")
public class EquipCateController {

    @Autowired
    CategoryService categoryService;

    @RequestMapping("/equipTypeList")
    public String equipTypeList(QueryInfo queryInfo){
        int numbers = categoryService.getEquipTypeCounts("%"+queryInfo.getQuery()+"%");
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<EquipCata> equipTypeList = categoryService.findEquipTypeByQuery("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String,Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",equipTypeList);
        System.out.println("总条数："+numbers);
        System.out.println(equipTypeList);
        return JSON.toJSONString(res);
    }

    @RequestMapping("/addEquipType")
    public String addEquipType(@RequestBody EquipCata equipCata){
        String status;
        EquipCata target = categoryService.findEquCateByName(equipCata.getEq_ca_name());
        if(target == null){
            //允许添加
            categoryService.addEquipCate(equipCata);
            status = "success";
        }else{
            status = "error";
        }
        return status;
    }

    @RequestMapping("/getUpdate")
    public String getUpdate(int id){
        EquipCata equipCata = categoryService.findEquCateById(id);
        return JSON.toJSONString(equipCata);
    }

    @RequestMapping("/editEquipType")
    public String editEquipType(@RequestBody EquipCata equipCata){
        categoryService.updateEquCate(equipCata);
        return "success";
    }

    @RequestMapping("/deleteEquipType")
    public String deleteEquipType(int id){
        categoryService.deleteEquCateById(id);
        return "success";
    }
}
